package com.example.tests;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.apache.commons.lang3.RandomStringUtils;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;

public class TestBase {
	
	protected static ApplicationManager app;

	@BeforeTest
	public void setUp() throws Exception {
		String configFile = System.getProperty("configFile", "application.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(configFile));
		app = new ApplicationManager(properties);
	}

	@AfterTest
	public void tearDown() throws Exception {
		app.stop();
	}
	
	@DataProvider
	public Iterator<Object[]> randomValidFolderNamesGenerator() {
		List<Object[]> folderNames = new ArrayList<Object[]>();
		for (int i = 0; i < 3; i++) {
			folderNames.add(new Object[]{generateRandomString(10)});
		}
		return folderNames.iterator();
	}
	
	public static String generateRandomString(int textLength) {
    	Random rnd = new Random();
		if (rnd.nextInt(100) == 0) {
			return "";
			} else {
				return RandomStringUtils.random(textLength, "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz");
			}
	}
}
